home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWResour / SLResour.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  8.8 KB  |  315 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                SLResour.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWRESFI_K
  13. #include "FWResFil.k"
  14. #endif
  15.  
  16. #ifndef FWRESFI_H
  17. #include "FWResFil.h"
  18. #endif
  19.  
  20. #ifndef   FWSTRS_H
  21. #include "FWStrs.h"
  22. #endif
  23.  
  24. #ifndef   FWFILESP_H
  25. #include "FWFileSp.h"
  26. #endif
  27.  
  28. #ifndef FWEXCDEF_H
  29. #include "FWExcDef.h"
  30. #endif
  31.  
  32. #ifndef   FWPRIDEB_H
  33. #include "FWPriDeb.h"
  34. #endif
  35.  
  36. #ifndef   FWMEMMGR_H
  37. #include "FWMemMgr.h"
  38. #endif
  39.  
  40. #ifndef FWEXCEPT_H
  41. #include "FWExcept.h"
  42. #endif
  43.  
  44.  
  45. #ifdef FW_BUILD_MAC
  46. #pragma segment fwresour
  47. #endif
  48.  
  49. /*
  50.  *  This file was generated by the SOM Compiler.
  51.  *  Generated using: 
  52.  *      SOM Emitter emitxtm.dll: 2.33
  53.  */
  54.  
  55. #define FW_OResource_Class_Source
  56. #include "SLResour.xih"
  57.  
  58. //========================================================================================
  59. // struct FW_CPrivResourceRep
  60. //    This struct contains the body of the 
  61. //========================================================================================
  62.  
  63. class FW_CPrivResourceRep
  64. {
  65. public:
  66.     FW_DECLARE_AUTO(FW_CPrivResourceRep)
  67.  
  68.     FW_CPrivResourceRep(Environment* ev,
  69.                         FW_OResourceFile* file,
  70.                         FW_ResourceID resourceID,
  71.                         FW_ResourceType resourceType);
  72.  
  73.     ~FW_CPrivResourceRep();
  74.  
  75.     FW_PResourceFile    fResourceFile;        // The resources file this resource originated from.
  76.     FW_ResourceID        fResourceID;        // The resource ID of the resource.
  77.     FW_ResourceType        fResourceType;        // The type of the resource.
  78.     FW_ResourceHandle    fResourceHandle;    // The native resource handle.
  79.     unsigned long        fResourceSize;        // The size of the resource, in bytes.
  80.  
  81. #ifdef FW_BUILD_MAC
  82.     unsigned short        fLockCount;            // Incremented by GetData, decremented by ReleaseData
  83.     FW_PlatformHandle    fResourceData;        // Resource data is stored here.
  84. #endif
  85.  
  86. #ifdef FW_BUILD_WIN
  87.     HGLOBAL                fLoadedHandle;
  88. #endif
  89. };
  90.  
  91.  
  92. FW_DEFINE_AUTO(FW_CPrivResourceRep)
  93.  
  94. //----------------------------------------------------------------------------------------
  95. // FW_CPrivResourceRep::FW_CPrivResourceRep
  96. //----------------------------------------------------------------------------------------
  97.  
  98. inline FW_CPrivResourceRep::FW_CPrivResourceRep(Environment* ev,
  99.                                                 FW_OResourceFile* file,
  100.                                                 FW_ResourceID resourceID,
  101.                                                 FW_ResourceType resourceType) :
  102.     fResourceFile(ev, file),
  103.     fResourceID(resourceID),
  104.     fResourceType(resourceType),
  105.     fResourceHandle(0)
  106. {
  107. #ifdef FW_BUILD_MAC
  108.     fLockCount = 0;
  109.     fResourceData = 0;
  110.     {
  111.         FW_CMacResLoadFalse dontLoadResource;
  112.         fResourceHandle = fResourceFile->GetResourceHandle(ev, fResourceID, fResourceType);
  113.     }
  114. #elif defined FW_BUILD_WIN
  115.     fResourceHandle = fResourceFile->GetResourceHandle(ev, fResourceID, fResourceType);
  116. #endif
  117.  
  118. #if defined FW_BUILD_MAC
  119.     fResourceSize = ::GetResourceSizeOnDisk(fResourceHandle);
  120. #elif defined FW_BUILD_WIN
  121.     fResourceSize = ::SizeofResource(fResourceFile->PrivGetResourceFileID(ev), fResourceHandle);
  122. #endif
  123.  
  124.     FW_END_CONSTRUCTOR
  125. }
  126.  
  127.  
  128. //----------------------------------------------------------------------------------------
  129. // FW_CPrivResourceRep::~FW_CPrivResourceRep
  130. //----------------------------------------------------------------------------------------
  131.  
  132. inline FW_CPrivResourceRep::~FW_CPrivResourceRep()
  133. {
  134.     FW_START_DESTRUCTOR
  135.  
  136. #ifdef FW_BUILD_MAC
  137.     FW_ASSERT(fLockCount == 0);
  138.     if (fResourceData != 0)
  139.         FW_CMemoryManager::FreeSystemHandle(fResourceData);
  140. #endif
  141.     FW_SOMEnvironment ev;
  142.     fResourceFile->ReleaseResourceHandle(ev, fResourceHandle);
  143. }
  144.  
  145.  
  146. //========================================================================================
  147. // CLASS FW_OResource
  148. //========================================================================================
  149.  
  150. //----------------------------------------------------------------------------------------
  151. // FW_OResource__InitFromFile
  152. //  Acquires the resource handle
  153. //----------------------------------------------------------------------------------------
  154.  
  155. SOM_Scope void  SOMLINK FW_OResource__InitFromFile(FW_OResource *somSelf, Environment *ev,
  156.         FW_OResourceFile* file,
  157.         FW_ResourceID resourceID,
  158.         FW_ResourceType resourceType)
  159. {
  160.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  161.  
  162.     FW_SOM_TRY
  163.     {
  164.         somThis->fRep = FW_NEW(FW_CPrivResourceRep, (ev, file, resourceID, resourceType));
  165.     }
  166.     FW_SOM_CATCH
  167. }
  168.  
  169.  
  170. //----------------------------------------------------------------------------------------
  171. // FW_OResource__AcquireData
  172. //  Lock the resource and return a pointer to the data.
  173. //  Client assumes responsiblity to call ReleaseData.
  174. //----------------------------------------------------------------------------------------
  175.  
  176. SOM_Scope void*  SOMLINK FW_OResource__AcquireData(FW_OResource *somSelf, Environment *ev)
  177. {
  178.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  179.  
  180.     FW_SOM_TRY
  181.     {
  182.         FW_CPrivResourceRep& rep = *somThis->fRep;
  183.         void *result;
  184.  
  185.         FW_ASSERT(rep.fResourceHandle != 0);
  186. #if defined FW_BUILD_MAC
  187.         FW_ASSERT(rep.fLockCount >= 0);
  188.         if (rep.fLockCount == 0)
  189.         {
  190.             if (rep.fResourceData == 0)
  191.             {
  192.                 // ODNewHandle throws kODErrOutOfMemory if it fails
  193.                 rep.fResourceData = FW_CMemoryManager::AllocateSystemHandle(rep.fResourceSize);
  194.     
  195.                 // Lock the handle, read the resource data
  196.                 FW_CMemoryManager::LockSystemHandle(rep.fResourceData);
  197.                 ::ReadPartialResource(rep.fResourceHandle, 0, *rep.fResourceData, rep.fResourceSize);
  198.     
  199.                 // Throw an exception if the read failed.
  200.                 FW_FailOnError(::ResError());
  201.             } 
  202.             else
  203.             {
  204.                 FW_CMemoryManager::LockSystemHandle(rep.fResourceData);
  205.             }
  206.         }
  207.         ++rep.fLockCount;
  208.         FW_ASSERT(*rep.fResourceData != 0);
  209.         result = *rep.fResourceData;
  210. #elif defined FW_BUILD_WIN
  211.         rep.fLoadedHandle = ::LoadResource(rep.fResourceFile->PrivGetResourceFileID(ev), rep.fResourceHandle);
  212.         result = ::LockResource(rep.fLoadedHandle);
  213. #endif
  214.  
  215.         return result;
  216.     }
  217.     FW_SOM_CATCH
  218.  
  219.     return 0;
  220. }
  221.  
  222.  
  223. //----------------------------------------------------------------------------------------
  224. // FW_OResource__ReleaseData
  225. //  Unlock the resource.
  226. //----------------------------------------------------------------------------------------
  227.  
  228. SOM_Scope void  SOMLINK FW_OResource__ReleaseData(FW_OResource *somSelf, Environment *ev)
  229. {
  230.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  231.  
  232.     FW_SOM_TRY
  233.     {
  234.         FW_CPrivResourceRep& rep = *somThis->fRep;
  235.  
  236. #if defined FW_BUILD_MAC
  237.         FW_ASSERT(rep.fLockCount > 0);
  238.         if (--rep.fLockCount == 0)
  239.         {
  240.             FW_CMemoryManager::UnlockSystemHandle(rep.fResourceData);
  241.             // (JEL) ??? Should we HPurge too?  My feeling is, tell developers they should
  242.             // generally set all resources to purgeable, but allow them to set a resource
  243.             // nonpurgeable for performance reasons.  If we always did an HPurge, we take
  244.             // control from the developer, for no good reason.
  245.         }
  246. #elif defined FW_BUILD_WIN16
  247.         ::UnlockResource(rep.fLoadedHandle);
  248. #endif
  249.     }
  250.     FW_SOM_CATCH
  251. }
  252.  
  253.  
  254. //----------------------------------------------------------------------------------------
  255. // FW_OResource__GetSize
  256. //  Return the size of this resource in bytes.
  257. //----------------------------------------------------------------------------------------
  258.  
  259. SOM_Scope unsigned long  SOMLINK FW_OResource__GetSize(FW_OResource *somSelf, Environment *ev)
  260. {
  261. FW_UNUSED(ev);
  262.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  263.  
  264.     return somThis->fRep->fResourceSize;
  265. }
  266.  
  267.  
  268. //----------------------------------------------------------------------------------------
  269. // FW_OResource__GetResourceHandle
  270. //  Return the platform (native) handle.
  271. //  This method should be used with caution, since it reveals platform specifics,
  272. //  and allows violation of internal lock counts, etc.
  273. //----------------------------------------------------------------------------------------
  274.  
  275. SOM_Scope FW_ResourceHandle  SOMLINK FW_OResource__GetResourceHandle(FW_OResource *somSelf, Environment *ev)
  276. {
  277. FW_UNUSED(ev);
  278.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  279.  
  280.     return somThis->fRep->fResourceHandle;
  281. }
  282.  
  283.  
  284. //----------------------------------------------------------------------------------------
  285. // FW_OResource__somInit
  286. //----------------------------------------------------------------------------------------
  287.  
  288. SOM_Scope void  SOMLINK FW_OResource__somInit(FW_OResource *somSelf)
  289. {
  290.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  291.  
  292. //    FW_OResource_parent_FW_ORefCount_somInit(somSelf);
  293.  
  294.     somThis->fRep = 0;
  295. }
  296.  
  297.  
  298. //----------------------------------------------------------------------------------------
  299. // FW_OResource__somUninit
  300. //----------------------------------------------------------------------------------------
  301.  
  302. SOM_Scope void  SOMLINK FW_OResource__somUninit(FW_OResource *somSelf)
  303. {
  304.     FW_OResourceData *somThis = FW_OResourceGetData(somSelf);
  305.  
  306.     FW_SOM_UNINIT_TRY
  307.     {
  308.         delete somThis->fRep;
  309. //        FW_OResource_parent_FW_ORefCount_somUninit(somSelf);
  310.     }
  311.     FW_SOM_UNINIT_CATCH
  312. }
  313.  
  314.  
  315.